﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;

namespace EnterpriseFund.Controllers
{
    public class PQManagerController : Controller
    {
        //
        // GET: /PQManager/
       
        public ActionResult Index()
        {
            List<Model.DM_PQ> pqlist = new List<Model.DM_PQ>();
            ViewData["blcode"] = true;
            BLL.DM_PQ pqbll = new BLL.DM_PQ();
            pqlist = pqbll.GetModelList("");

            return View(pqlist);
        }
        public ActionResult PQManageListPartial()
        {
            List<Model.DM_PQ> pqlist = new List<Model.DM_PQ>();
            ViewData["blcode"] = true;
            BLL.DM_PQ pqbll = new BLL.DM_PQ();
            pqlist = pqbll.GetModelList("");

            return PartialView("PQManageListPartial", pqlist);
        }
        public ActionResult DM_PQEdit(string PQ_DM)
        {
            ViewData["blcodeNo"] = true;
            Model.DM_PQ mpq = new Model.DM_PQ();
            BLL.DM_PQ pqbll = new BLL.DM_PQ();

            if (!string.IsNullOrEmpty(PQ_DM))
            {
                mpq = pqbll.GetModel(PQ_DM);
                if (mpq == null)
                {
                    mpq = new Model.DM_PQ();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }

            }
            else
            {
                mpq = new Model.DM_PQ();
            }

            return View(mpq);
        }

        public string DelPQ(string PQ_DM)
        {
            string str = string.Empty;
            if (null != PQ_DM)
            {
                PQ_DM = Fetch.estimateString(PQ_DM, CheckGetEnum.Safety);
                BLL.DM_PQ codeNameBll = new BLL.DM_PQ();
                try
                {
                    codeNameBll.Delete(PQ_DM);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }
        public string HYSave(string HY_DM, string HY_MC, string HY_JC, string HYDL_DM, string GS_HY_MC, string YXBZ, bool isAdd)
        {
            string str = string.Empty;

            HY_DM = Fetch.estimateString(HY_DM, CheckGetEnum.Safety);
            HY_MC = Fetch.estimateString(HY_MC, CheckGetEnum.Safety);
            HY_JC = Fetch.estimateString(HY_JC, CheckGetEnum.Safety);
            HYDL_DM = Fetch.estimateString(HYDL_DM, CheckGetEnum.Safety);
            GS_HY_MC = Fetch.estimateString(GS_HY_MC, CheckGetEnum.Safety);
            YXBZ = Fetch.estimateString(YXBZ, CheckGetEnum.Safety);
            BLL.DM_HY codeNameBll = new BLL.DM_HY();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(HY_DM))
                {
                    return "exists";
                }
            }

            Model.DM_HY codeNameObj = new Model.DM_HY();
            codeNameObj.HY_DM = HY_DM;
            codeNameObj.HY_MC = HY_MC;
            codeNameObj.HY_JC = HY_JC;
            codeNameObj.HYDL_DM = HYDL_DM;
            codeNameObj.GS_HY_MC = GS_HY_MC;
            codeNameObj.YXBZ = YXBZ;
            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                str = "ok";

            }
            catch (Exception)
            {

                str = "error";
            }

            return str;
        }
        public ActionResult SavaModel(Model.DM_PQ p)
        {
             try
 {
	 BLL.DM_PQ codeNameBll = new BLL.DM_PQ();
     ViewData["blcodeNo"] = true;
	            bool btbYXBZ = CheckBoxExtension.GetValue<bool>("tbYXBZ");
	          bool btbXYBZ=  CheckBoxExtension.GetValue<bool>("tbXYBZ");
	          if (btbYXBZ==null)
	          {
	              ViewBag.Msg="错误:有效 标志不存在";
                  return View("DM_PQEdit",p);
	          }
	            if (btbXYBZ==null)
	            {
	                ViewBag.Msg="错误:选用标志不存在";
                    return View("DM_PQEdit",p);
	            }
	            p.YXBZ = btbYXBZ ? "Y" : "N";
	            p.XYBZ = btbXYBZ ? "Y" : "N";
	            if (codeNameBll.Exists(p.PQ_DM))
	            {
	                codeNameBll.Update(p);
                    ViewBag.Msg = "ok";
                    return View("DM_PQEdit",p);
	            }
	            else
	            {
	                codeNameBll.Add(p);
                    ViewBag.Msg = "ok";
                    return View("DM_PQEdit",p);
	            }
 }
 catch (System.Exception ex)
 {
     ViewBag.Msg = "错误:"+ex.Message.ToString();
     return View("DM_PQEdit",p);
 }
            
        }
    }
}
